package com.shuaiche.yeguan.util;

import android.util.Log;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by baoyinbo on 2017/11/17.
 */

public class SCTimeUtils {

    public static String compareTime(String clickTime) {
        String r = "";
        try {
            if (clickTime == null) {return r;}

            long nowTime = System.currentTimeMillis();
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date d = df.parse(clickTime);

//            long time = 86400000l * 365;
            long clickTimeLong = d.getTime();
            long result = Math.abs(nowTime - clickTimeLong);
            // 一小时内
            if (result < 3600000) {
                long seconds = result / 60000;
                r = seconds + 1 + "";
            }
            return r;
        } catch (ParseException e) {
            e.printStackTrace();
            r = "";
//            r = clickTime;
            return r;
        }
    }


    /**
     * 显示几天前
     *
     * @param ctime
     * @return
     */
    public static String showTime(String ctime) {
        String r = "";
        try {
            if (ctime == null) return r;
            //        if(format==null)format="yyyy-MM-dd HH:mm:ss";
            long nowtimelong = System.currentTimeMillis();
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            DateFormat ds = new SimpleDateFormat("yyyy");
            Date date = new Date();
            String timeNow = date.toLocaleString();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(ds.parse(timeNow));
            int yearNow = calendar.get(Calendar.YEAR);
            Calendar calendar1 = Calendar.getInstance();
            calendar1.setTime(ds.parse(ctime));
            int yearOld = calendar1.get(Calendar.YEAR);
            Date d = null;
            d = df.parse(ctime);

            long time = 86400000l * 365;
            long ctimelong = d.getTime();
            long result = Math.abs(nowtimelong - ctimelong);
            Log.d("时间格式+", "showTime: " + result);

            if (result < 60000)// 一分钟内
            {
                long seconds = result / 1000;
                r = "刚刚";
            } else if (result >= 60000 && result < 3600000)// 一小时内
            {
                long seconds = result / 60000;
                r = seconds + "分钟前";
            } else if (result >= 3600000 && result < 86400000)// 一天内
            {
                long seconds = result / 3600000;
                r = seconds + "小时前";
            } else if (result >= 86400000 && result < Math.abs(time)) //一年内
            {
                if (yearNow - yearOld >= 1) {
                    r = new SimpleDateFormat("yyyy-MM-dd").format(ctimelong);
                } else {
                    r = new SimpleDateFormat("MM-dd").format(ctimelong);
                }
            } else if (result >= Math.abs(time)) {
                r = new SimpleDateFormat("yyyy-MM-dd").format(ctimelong);
            }


        } catch (ParseException e) {
            e.printStackTrace();
            r = ctime;
            return r;
        }
        return r;
    }


    /**
     * 当天显示 上午 8：00 下午 23：00
     * 昨天 或 以前  显示日期 2018-01-01
     *
     * @param ctime
     * @return
     */
    public static String showDateTime(String ctime) {
        return showTime(ctime);
    }

    public static String showSubDateTime(String ctime) {
        String r = "";
        try {
            if (StringUtils.isEmpty(ctime)) return r;
            long nowtimelong = System.currentTimeMillis();
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            Date d = null;
            d = df.parse(ctime);
            long ctimelong = d.getTime();
            long ds = nowtimelong - ctimelong;  //毫秒数
            if (ds < 60 * 60 * 1000) {
                return ds / (60 * 1000) + "分钟前";
            } else if (ds < 24 * 60 * 60 * 1000) {
                return ds / (60 * 60 * 1000) + "小时前";
            } else {
                return new SimpleDateFormat("MM-dd").format(d);
            }

        } catch (ParseException e) {
            e.printStackTrace();
            r = ctime;
            return r;
        }
    }


    /**
     * 转换格式为 yyyy年MM月dd日
     *
     * @param time
     * @return
     */
    public static String parseYearMonthDay(String time) {
        String strDate = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date date = sdf.parse(time);
            strDate = sdf.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return strDate;
    }

    /**
     * 转换格式为 yyyy年MM月
     *
     * @param time
     * @return
     */
    public static String parseYearMonth(String time) {
        String strDate = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            Date date = sdf.parse(time);
            strDate = sdf.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return strDate;
    }

    /**
     * 转换格式为 yyyy年MM月dd日
     *
     * @param time
     * @return
     */
    public static String parseYear(String time) {
        String strDate = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
            Date date = sdf.parse(time);
            strDate = sdf.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return strDate;
    }

    /**
     * 转换格式为 MM月dd日
     *
     * @param time
     * @return
     */
    public static String parseMonthDay(String time) {
        String strDate = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat sdf1 = new SimpleDateFormat("M月dd日");
            Date date = sdf.parse(time);
            strDate = sdf1.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return strDate;
    }


    public static void main(String args[]) {
        System.out.print(showDateTime("2017-11-27 13:00:00"));

    }

    /**
     * 根据年月返回最后一天 yyyy-MM-dd
     *
     * @param sDate
     * @return
     */
    public static String getLastDayOfMonth(Date sDate) {
        Calendar cDay1 = Calendar.getInstance();
        cDay1.setTime(sDate);
        int lastDay = cDay1.getActualMaximum(Calendar.DAY_OF_MONTH);
        Date lastDate = cDay1.getTime();
        lastDate.setDate(lastDay);

        SimpleDateFormat sdfdd = new SimpleDateFormat("yyyy-MM-dd");
        return sdfdd.format(lastDate);
    }


    /**
     * 0:正常
     * 1:黄色报警
     * 2:红色报警
     *
     * @param createTime
     * @return
     */



    /**
     * 获取库龄
     *
     * @param createTime
     * @return
     */
    public static int getStockAge(String createTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = sdf.parse(createTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long s1 = date.getTime();               //将时间转为毫秒
        long s2 = System.currentTimeMillis();   //得到当前的毫秒
        int day = (int) ((s2 - s1) / 1000 / 60 / 60 / 24);  //距离当前时间天数

        return day + 1;
    }


    public static String parseTimeYYMMDDHHMMSStoMMDDHHMM(String createTime) {
        String strDate = null;
        try {
//            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = sdf.parse(createTime);
            DateFormat format = new SimpleDateFormat("MM-dd HH:mm");
            strDate = format.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return strDate;
    }


    /**
     * 获取当前星期几
     *
     * @return
     */
    public static String getWeek() {
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar calendar = Calendar.getInstance();
        return weekDays[calendar.get(Calendar.DAY_OF_WEEK) - 1];
    }

    /**
     * 获取当前月份
     *
     * @return
     */


    public static String getMonth() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.MONTH) + 1 + "月";
    }

    public static int getDay() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.DATE);
    }

    /**
     * 计算车龄
     *
     * @param data
     * @return
     */
    public static int getDifferMonth(String data) {
        int all = 0;
        try {
            Date curDate = new Date(System.currentTimeMillis());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            String time = sdf.format(curDate);
            Calendar bef = Calendar.getInstance();
            Calendar aft = Calendar.getInstance();
            aft.setTime(sdf.parse(time));
            bef.setTime(sdf.parse(data));
            if (aft.get(Calendar.YEAR) == bef.get(Calendar.YEAR)) {
                all = aft.get(Calendar.MONTH) - bef.get(Calendar.MONTH) + 1;
            } else {
                all = aft.get(Calendar.MONTH) + 13 - bef.get(Calendar.MONTH);
                all += (aft.get(Calendar.YEAR) - 1 - bef.get(Calendar.YEAR)) * 12;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return all;
    }
}
